package com.gift.recommendation.modules.admin.biz;

import com.gift.recommendation.Base.PageData;
import com.gift.recommendation.util.PageTransUtil;
import com.gift.recommendation.modules.admin.dao.entity.NoticeEntity;
import com.gift.recommendation.modules.admin.dto.NoticeListReq;
import com.gift.recommendation.modules.admin.dto.NoticeRsp;
import com.gift.recommendation.modules.admin.dto.NoticeReleaseAddReq;
import com.gift.recommendation.modules.admin.service.NoticeService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author zzh <2658137258@qq.com>
 * @date 2023/1/15
 */
@Service
public class NoticeImpl {

    @Autowired
    private NoticeService noticeService;

    /**
     * 添加公告
     * @param req
     */
    public void addNotice(NoticeReleaseAddReq req) {
        NoticeEntity notice = new NoticeEntity();
        notice.setContent(req.getContent());
        notice.setTitle(req.getTitle());
        notice.setStatus(1);
        noticeService.insertSelective(notice);
    }

    /**
     * 删除公告
     * @param id
     */
    public void deleteNotice(Long id) {
        noticeService.deleteByPrimaryKey(id);
    }

    /**
     * 发布公告
     * @param id
     */
    @Transactional
    public void releaseNotice(Long id){
        Example example = new Example(NoticeEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",2);
        List<NoticeEntity> list = noticeService.selectByExample(example);
        if (list.size() > 0){
            list.forEach(s->{
                NoticeEntity tmp = new NoticeEntity();
                tmp.setId(s.getId());
                tmp.setStatus(1);
                noticeService.updateByPrimaryKeySelective(tmp);
            });
        }
        NoticeEntity notice = new NoticeEntity();
        notice.setId(id);
        notice.setStatus(2);
        noticeService.updateByPrimaryKeySelective(notice);
    }

    /**
     * 获取在线的通知
     * @return
     */
    public NoticeRsp checkNotice() {
        Example example = new Example(NoticeEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",2);
        List<NoticeEntity> list = noticeService.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            NoticeRsp noticeRsp = new NoticeRsp();
            BeanUtils.copyProperties(list.get(0),noticeRsp);
            return noticeRsp;
        } else {
            return null;
        }
    }

    /**
     * 分页查询
     * @param listReq
     * @return
     */
    public PageData<NoticeRsp> list(NoticeListReq listReq) {
        Example example = new Example(NoticeEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(listReq.getTitle())){
            criteria.andLike("title","%" + listReq.getTitle() + "%");
        }
        if (StringUtils.isNotBlank(listReq.getContent())){
            criteria.andLike("content","%" + listReq.getContent() + "%");
        }
        if(listReq.getStartTime() != null && listReq.getEndTime() != null){
            criteria.andBetween("createTime",listReq.getStartTime(),listReq.getEndTime());
        }
        if (listReq.getStartTime() != null && listReq.getEndTime() == null){
            criteria.andGreaterThanOrEqualTo("createTime",listReq.getStartTime());
        }
        if (listReq.getStartTime() == null && listReq.getEndTime() != null){
            criteria.andLessThanOrEqualTo("createTime",listReq.getEndTime());
        }
        example.setOrderByClause("update_time DESC");
        com.github.pagehelper.Page<NoticeRsp> page = PageHelper.startPage(
                listReq.getCurrentPage(), listReq.getPageSize()
        );
        noticeService.selectByExample(example);
        return PageTransUtil.createPageList(page, NoticeRsp.class);
    }

    /**
     * 根据主键id查找通知
     * @param id
     * @return
     */
    public NoticeRsp releaseSearchById(Long id) {
        NoticeRsp noticeRsp = new NoticeRsp();
        BeanUtils.copyProperties(noticeService.selectByPrimaryKey(id),noticeRsp);
        return noticeRsp;
    }

    /**
     * 更新通知
     * @param req
     */
    public void noticeUpdate(NoticeReleaseAddReq req) {
        NoticeEntity notice = new NoticeEntity();
        BeanUtils.copyProperties(req,notice);
        noticeService.updateByPrimaryKeySelective(notice);
    }

    /**
     * 下线通知
     * @param id
     */
    public void offlineReleaseNotice(Long id) {
        NoticeEntity notice = new NoticeEntity();
        notice.setId(id);
        notice.setStatus(1);
        noticeService.updateByPrimaryKeySelective(notice);
    }
}
